/**
 * 
 */
package com.flymobi.util;

import java.util.ArrayList;
import java.util.List;

import com.flymobi.fw.modules.SM;
import com.flymobi.fw.modules.cache.CacheNeedsRefreshException;
import com.flymobi.po.FriendsLink;
import com.flymobi.po.PageBlock;
import com.flymobi.po.PageBlockContent;
import com.flymobi.po.Pindao;

/**
 * @author Administrator
 * 
 */
public class DE {
	private Long pindaoId;
	private Long blockId;

	/**
	 * @return the pindaoId
	 */
	public Long getPindaoId() {
		return pindaoId;
	}

	/**
	 * @param pindaoId
	 *            the pindaoId to set
	 */
	public void setPindaoId(Long pindaoId) {
		this.pindaoId = pindaoId;
	}

	/**
	 * @return the blockId
	 */
	public Long getBlockId() {
		return blockId;
	}

	/**
	 * @param blockId
	 *            the blockId to set
	 */
	public void setBlockId(Long blockId) {
		this.blockId = blockId;
	}

	@SuppressWarnings("unchecked")
	public List<PageBlockContent> getList() {
		List<PageBlockContent> list;
		if (blockId == null) {
			return null;
		}
		PageBlock pb = null;
		try {
			pb = (PageBlock)SM.CacheOscache.getFromCache("PageBlock", blockId+"", 10*60);
		} catch (CacheNeedsRefreshException e) {
			pb=SM.PageBlockService.get(blockId + "");
			SM.CacheOscache.putInCache("PageBlock", blockId+"",  pb);
		}
		if(pb==null){
			pb=SM.PageBlockService.get(blockId + "");
		}
		
		if(pb==null){
			return null;
		}
		
		if (pindaoId == null) {
			try {
				list = (List<PageBlockContent>) SM.CacheOscache.getFromCache(
						"blocklist", blockId + "", 10 * 60);
			} catch (CacheNeedsRefreshException e) {
				list = SM.PageBlockContentService.list(blockId, 0, pb
						.getShowCount() == 0 ? 1000 : pb.getShowCount()
						.intValue());
				SM.CacheOscache.putInCache("blocklist", blockId + "", list);
			}
		} else {
			try {
				list = (List<PageBlockContent>) SM.CacheOscache.getFromCache(
						"blocklist", blockId + "-" + pindaoId, 10 * 60);
			} catch (CacheNeedsRefreshException e) {
				list = SM.PageBlockContentService.list(blockId, pindaoId, 0, pb
						.getShowCount() == 0 ? 1000 : pb.getShowCount()
						.intValue());
				SM.CacheOscache.putInCache("blocklist", blockId + "-"
						+ pindaoId, list);
			}
		}
		if(list!=null){
			for(PageBlockContent pbc:list){
				if("2".equals(pbc.getType())){
					if(pbc.getAd()!=null){
						if(pbc.getAd().getStatus()!=1){
							list.remove(pbc);
						}
					}
				}
			}
		}
		pindaoId = null;
		blockId = null;
		if(list==null){
			list=new ArrayList<PageBlockContent>();
		}
		return list;
	}

	public String getName() {

		PageBlock pb;
		try {
			pb = (PageBlock) SM.CacheOscache.getFromCache("blockName", blockId
					+ "", 10 * 60);
		} catch (CacheNeedsRefreshException e) {
			pb = SM.PageBlockService.get(blockId + "");
			SM.CacheOscache.putInCache("blockName", blockId + "", pb);
		}
		if (pb == null) {
			return null;
		}
		return pb.getBlockName();
	}

	@SuppressWarnings("unchecked")
	public List<Pindao> getPindaoList() {
		List<Pindao> list;
		try {
			list = (List<Pindao>) SM.CacheOscache.getFromCache("pindaolist",
					"", 10 * 60);
		} catch (CacheNeedsRefreshException e) {
			list = SM.PindaoService.listShow();
			SM.CacheOscache.putInCache("pindaolist", "", list);
		}

		return list;
	}

	@SuppressWarnings("unchecked")
	public List<Pindao> getLanmuList() {
		List<Pindao> lanmuList;
		try {
			lanmuList = (List<Pindao>) SM.CacheOscache.getFromCache("lanmulist",
					pindaoId+"", 10 * 60);
		} catch (CacheNeedsRefreshException e) {
			lanmuList = SM.PindaoService.listShow(new Long(pindaoId));
			SM.CacheOscache.putInCache("lanmulist",
					pindaoId+"", lanmuList);
		}
		
		pindaoId = null;
		return lanmuList;
	}

	public List<FriendsLink> getFriendsLinkList() {
		List<FriendsLink> list = SM.FriendsLinkService.list(0, 100);

		return list;
	}
}
